package com.hw.one.dam2.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hw.one.ext.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hw.one.dam2.dao.IGridMemberDao;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.ext.util.UserUtil;

/**
 * Copyright 2017 汉威.智慧环保事业部
 * Created by 毛超然 on 2017/6/28 0009.
 */

@Service(value = "countyGridService")
public class CountyGridService implements ICountyGridService {

    @Autowired
    private IRegionSiteDao regionSiteDao;

    @Autowired
    private IGridMemberDao gridMemberDao;

    /**
     * 分级别查询村镇组信息
     *
     * @param paraMap
     * @return
     * @throws Exception
     */

    @Override
    public List<Map> findSubRegionDataByLevel(Map<String, String> paraMap) throws Exception {
        // 根据登录用户，查询用户所属行政区域
        Map<String, Object> region = UserUtil.getRegion();
        String userRegionCode = (String) region.get("regionCode");

        String level = paraMap.get("level");
        int regionType = 0;
        int regionLevel = 0;
        if ("county".equals(level)) {
            regionType = 24;
            regionLevel = 3;
        } else if ("town".equals(level)) {
            regionType = 30;
            regionLevel = 4;
        } else if ("village".equals(level)) {
            regionType = 40;
            regionLevel = 5;
        } else if ("group".equals(level)) {
            regionType = 50;
            regionLevel = 6;
        } else {
        }
        //封装参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("regionType", regionType);
        paramMap.put("regionCode", userRegionCode);
        paramMap.put("regionLevel", regionLevel);
        paramMap.put("regionName", paraMap.get("regionName"));

        List<Map<String, Object>> subRegionList = regionSiteDao.findSubRegionInfoByRegionCode(paramMap);

        List<Map> resultList = new ArrayList<>();
        Map<String, String> map = null;
        for (Map<String, Object> resMap : subRegionList) {
            map = new HashMap<>();
            map.put("regionCode", String.valueOf(resMap.get("regionCode")));
            map.put("regionName", String.valueOf(resMap.get("regionName")));
            map.put("regiontype", String.valueOf(resMap.get("regionType")));
            map.put("longitude", null == resMap.get("longitude") ? "" : String.valueOf(resMap.get("longitude")));
            map.put("latitude", null == resMap.get("latitude") ? "" : String.valueOf(resMap.get("latitude")));
            map.put("remark", null == resMap.get("remark") ? "" : String.valueOf(resMap.get("remark")));

            resultList.add(map);
        }

        return resultList;
    }

    /**
     * 根据区域编码，获取网络成员的信息
     *
     * @param regionCode
     * @return
     * @throws Exception
     */

    @Override
    public Map<String, List<Map>> findGridMemberListMapByParentCode(String regionCode) throws Exception {
        List<Map<String, Object>> subRegionList = gridMemberDao.findGridMemberListMapByParentCode(regionCode);

        Map<String, List<Map>> resultMap = new HashMap<>();

        List<Map> adminList = new ArrayList<>(); // 网格长
        List<Map> deputyList = new ArrayList<>(); // 副网格长
        List<Map> memberList = new ArrayList<>(); // 网格员
        List<Map> inspectorList = new ArrayList<>(); // 巡查员
        List<Map> superviseList = new ArrayList<>(); // 监督员
        List<Map> powerList = new ArrayList<>(); // 供电员

        Map<String, String> map = null;
        String gridRole = "";
        for (Map<String, Object> resMap : subRegionList) {
            map = new HashMap<>();
            gridRole = String.valueOf(resMap.get("gridRole"));
            map.put("name", String.valueOf(resMap.get("name")));
            map.put("gridRole", gridRole);
            map.put("regionCode", String.valueOf(resMap.get("regionCode")));
            map.put("duty", null == resMap.get("duty") ? "" : String.valueOf(resMap.get("duty")));
            map.put("mobilePhone", null == resMap.get("mobilePhone") ? "" : String.valueOf(resMap.get("mobilePhone")));
            map.put("orderNumber", String.valueOf(resMap.get("orderNumber")));
            map.put("dutyDetails", null == resMap.get("dutyDetails") ? "" : String.valueOf(resMap.get("dutyDetails")));
            if ("1".equals(gridRole)) {
                adminList.add(map);
            } else if ("2".equals(gridRole)) {
                deputyList.add(map);
            } else if ("3".equals(gridRole)) {
                memberList.add(map);
            } else if ("4".equals(gridRole)) {
                inspectorList.add(map);
            } else if ("5".equals(gridRole)) {
                superviseList.add(map);
            } else if ("6".equals(gridRole)) {
                powerList.add(map);
            }

        }
        resultMap.put("adminList", adminList);
        resultMap.put("deputyList", deputyList);
        resultMap.put("memberList", memberList);
        resultMap.put("inspectorList", inspectorList);
        resultMap.put("superviseList", superviseList);
        resultMap.put("powerList", powerList);

        return resultMap;
    }


    @Override
    public List<Map<String, Object>> findEnterpriseListByRegionCode() throws Exception {
        // 根据登录用户，查询用户所属行政区域
        Map<String, Object> region = UserUtil.getRegion();
        String userRegionCode = (String) region.get("regionCode");

        List<Map<String, Object>> regionInfoList = regionSiteDao.findSonRegionInfoByRegionCode(userRegionCode);
        List<String> regionCodeList = new ArrayList<>();
        for (Map<String, Object> regionInfo : regionInfoList) {
            regionCodeList.add((String) regionInfo.get("regionCode"));
        }

        List<Map<String, Object>> enterpriseList = gridMemberDao.findEnterpriseListByRegionCode(regionCodeList);

        return enterpriseList;

    }

}
